import axios from 'axios';

const BASE_API =
  process.env.NODE_ENV === 'development' ? '' : 'http://127.0.0.1:8020';

async function sendChunkedData(url, data, chunkSize = 1024 * 1024) {
  let offset = 0;
  let chunk;
  do {
    chunk = data.substr(offset, chunkSize);
    offset += chunkSize;

    console.log(Buffer.byteLength(chunk));

    const headers = {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(data, 'utf8'), // 指定发送的数据长度
      'Content-Range': `bytes ${offset - chunkSize}-${offset -
        1}/${Buffer.byteLength(data)}`, // 指定当前数据块在总数据中的位置和长度
      // 其他请求头部信息
    };

    try {
      const response = await axios.post(BASE_API + url, chunk, { headers });
      console.log(response.data);
    } catch (error) {
      console.error(error);
    }
  } while (chunk.length === chunkSize);
}

export default sendChunkedData